package org.zoomdev.zoom.common.utils;

import org.zoomdev.zoom.common.caster.Caster;
import org.zoomdev.zoom.common.exceptions.ZoomException;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class Utils {


    public static boolean containTypeParameters(Type type) {
        if (type instanceof Class) {
            return false;
        }

        if (type instanceof ParameterizedType) {
            ParameterizedType p = (ParameterizedType) type;
            if (containTypeParameters(p.getRawType())) {
                return true;
            }
            if (p.getActualTypeArguments() != null) {
                for (Type tt : p.getActualTypeArguments()) {
                    if (containTypeParameters(tt)) {
                        return true;
                    }
                }
            }

        }

        return type instanceof TypeVariable;
    }


    public static Collection newCollection(Class<?> type) {
        if (type.isInterface()) {
            if (List.class.isAssignableFrom(type)) {
                return new ArrayList();
            } else if (Set.class.isAssignableFrom(type)) {
                return new LinkedHashSet();
            } else if (Collection.class.isAssignableFrom(type)) {
                return new ArrayList();
            } else if (Iterable.class.isAssignableFrom(type)) {
                return new ArrayList();
            } else {
                throw new ZoomException("Cannot initialize " + type);
            }
        } else {
            return (Collection) Classes.newInstance(type);
        }
    }

    public static boolean isAbstract(Class type) {
        assert (type != null);
        return Modifier.isAbstract(type.getModifiers());
    }

    /**
     * 设置某个字段的值
     *
     * @param clazz
     * @param instance
     * @param name
     * @param value
     * @throws Exception
     */
    public static void set(Class<?> clazz, Object instance, String name, Object value) {
        try {
            Field field = Classes.fetchField(clazz, name);
            field.setAccessible(true);
            field.set(instance, value);
        } catch (Exception e) {
            throw new ZoomException(e);
        }
    }


    public static Class<?> getWrapClass(Class<?> type) {
        if (type == int.class) {
            return Integer.class;
        }
        if (type == boolean.class) {
            return Boolean.class;
        }
        if (type == double.class) {
            return Double.class;
        }
        if (type == float.class) {
            return Float.class;
        }
        if (type == long.class) {
            return Long.class;
        }
        if (type == short.class) {
            return Short.class;
        }
        if (type == byte.class) {
            return Byte.class;
        }
        if (type == char.class) {
            return Character.class;
        }
        throw new Caster.CasterException("impossible");
    }
}
